


#import "UIUtil.h"

@implementation UIUtil

+ (UIColor *) colorWithHexString: (NSString *) stringToConvert
{
    NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) return [UIColor blackColor];
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
    if ([cString length] != 6) return [UIColor blackColor];
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f)
                           green:((float) g / 255.0f)
                            blue:((float) b / 255.0f)
                           alpha:1.0f];
}

+ (void)archiveDataToDefaults:(id)data withKey:(NSString *)key {
    key = [NSString stringWithFormat:@"%@", key];
    SET_DEFAULTS(Object, key, [NSKeyedArchiver archivedDataWithRootObject:data]);
}

+ (id)unarchiveDataFromDefaultsWithKey:(NSString *)key {
    key = [NSString stringWithFormat:@"%@", key];
    NSData *encodedObject = DEFAULTS(object, key);
    return encodedObject ? [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject] : nil;
}

+ (void)removeArchivedDataWithKey:(NSString *)key {
    key = [NSString stringWithFormat:@"%@", key];
    RM_DEFAULTS(key);
}

+ (BOOL)isNetworkReachable {
    return [AFNetworkReachabilityManager sharedManager].reachable;
}

+ (UIViewController *)viewControllerHasNavgation:(UIView *)aView
{
    for (UIView *next = aView.superview; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            if (((UIViewController*)nextResponder).navigationController) {
                return (UIViewController*)nextResponder;
            }
        }
    }
    return nil;
}


// 自定义textField
+ (void)configTextField:(UITextField *)textField withTitle:(NSString *)string {
    textField.leftViewMode = UITextFieldViewModeAlways;
    UILabel *leftLabel = [[UILabel alloc] init];
    if ([string isEqualToString:@""]) {
        leftLabel.frame = CGRectMake(0, 0, 5, 40);
    } else {
        leftLabel.frame = CGRectMake(0, 0, 75, 40);
    }
    leftLabel.text = [NSString stringWithFormat:@"  %@", string];
    leftLabel.textColor = [UIUtil colorWithHexString:appFontColor];
    leftLabel.font = [UIFont systemFontOfSize:15];
    [textField leftViewRectForBounds:CGRectMake(0, 0, 75, 40)];
    textField.leftView = leftLabel;
}

+ (NSDateComponents *)date {
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    return dateComponent;
}

+ (NSDictionary *)toJsonString:(id)parama {
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parama options:2 error:&error];
    if ([jsonData length] > 0 && error == nil) {
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSDictionary *para = @{@"data":jsonString};
        return para;
    } else {
        return nil;
    }
}

+ (NSString *)containString:(NSString *)string {
    NSRange range = [string rangeOfString:@"http://"];
    if (range.location == NSNotFound) {
        string = [@"http://" stringByAppendingString:string];
    }
    return string;
}

+ (void)tokenOverdue:(UIViewController *)vc msg:(NSString *)msg{
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[LoginViewController new]];
    vc.view.window.rootViewController = nav;
    [UserModel sharedInstance].userName = nil;
    [UserModel sharedInstance].userToken = nil;
    if ([msg isEqualToString:@"9001"]) {
        [[ESHUDView sharedInstance] showErrorMessage:@"token已过期请重新登录" dismissBlock:nil];
    } else if ([msg isEqualToString:@"9002"]) {
        [[ESHUDView sharedInstance] showErrorMessage:@"用户名不存在请重新登录" dismissBlock:nil];
    }
}

@end
